//
//  AddBankViewController.m
//  Kfangchan
//
//  Created by 朋飞 on 14-11-13.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "AddBankViewController.h"
#pragma mark 添加银行卡号
#pragma mark
@interface AddBankViewController ()
@property (strong, nonatomic) IBOutlet UIButton *saveBtn;
@property  (strong, nonatomic) NSString *bankNo1;
@property (strong, nonatomic)  NSString *bankName;
@property  (strong, nonatomic) NSString *bankPerson;

@property (strong, nonatomic) IBOutlet UITextField * banknameTextField;
@property (strong, nonatomic) IBOutlet UITextField * personnameTextField;
@property (strong, nonatomic) IBOutlet UITextField * banknumTextField;




@end

@implementation AddBankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 返回
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"添加银行账号"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    
    self.navigationItem.rightBarButtonItem = [[Theam currentTheam] navigationBarRightButtonItemWithImage:nil Title:@"保存" Target:self Selector:@selector(save)];
    
    self.bankNo1 = [AppConfig getAgent][AGENT_bankNo];
    self.bankName =  [AppConfig getAgent][AGENT_bankName];
    self.bankPerson=   [AppConfig getAgent][ AGENT_bankPayee];
    self.banknumTextField.text =self.bankNo1;
    self.personnameTextField.text =self.bankPerson;
    self.banknameTextField.text =self.bankName;
    
    
    
    
}
#pragma mark  保存
#pragma mark
-(void) save {
    BOOL  verify = [self verify];
    if(verify){
        //弱引用
        __weak AddBankViewController *weak_self = self;
        // 构建请求参数
        NSDictionary *param = @{
                                @"bankName":self.banknameTextField.text,
                                @"bankPayee":self.personnameTextField.text,
                                @"bankNo":self.banknumTextField.text
                                };
        // 请求
        MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_UPDATE completionHandler:^(id responseObject) {
            
            // NSDictionary* dic = [AppConfig getAgent];
            NSMutableDictionary* dic = [[AppConfig getAgent] mutableCopy];
            NSString *bankname = [NSString stringWithString:self.banknameTextField.text];
            [dic setValue: bankname forKey:@"bankName"];
            NSString *strNo= [NSString stringWithString:self.banknumTextField.text];
            [dic setValue: strNo forKey:@"bankNo"];
            NSString *strname = [NSString stringWithString:self.personnameTextField.text];
            [dic setValue: strname forKey:@"bankPayee"];
            [AppConfig setAgent:dic];
            [weak_self back];
            
        } errorHandler:^(NSError *error){
            
            showCustomAlertMessage(@"网路错误");
            
        }finishHandler:^(id responseObject) {
            
        }];
        
        [self.opHandlers addObject:operation];
        
    }
    
}


#pragma mark -
#pragma mark 关闭键盘
- (IBAction)hideKeyboard {
    [self.banknameTextField resignFirstResponder];
    [self.banknumTextField resignFirstResponder];
    [self.personnameTextField resignFirstResponder];
    
}

-(BOOL) verify{
    NSString *bankname = self.banknameTextField.text;
    NSString *banknum = self.banknumTextField.text;
    NSString *personname = self.personnameTextField.text;
    
    
    UIAlertView *alert = nil;
    
    
    if(bankname.length==0){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入银行名称" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    if(banknum.length==0){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入银行卡账号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    if(!bankCard(banknum)){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入正确的银行卡账号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    
    if(personname.length==0){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入银行卡姓名" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    if([self.bankName isEqualToString: bankname]&&[self.bankPerson isEqualToString:personname]&&[self.bankNo1 isEqualToString:banknum]){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"没有任何修改" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];

        return NO;
    }
    
    //    if(password.length<6){
    //        alert = [[UIAlertView alloc]initWithTitle:nil message:@"用户名不少于6个字符" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    //        [alert show];
    //        return NO;
    //    }
    //
    
    
    
    
    return YES ;
}


#pragma mark 返回
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
